package com.wwwarehouse.taskcenter.fragment.job_point.step5;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.eventbus_event.BackListenerEvent;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;
import com.wwwarehouse.taskcenter.eventbus_event.TaskPointCardEvent;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * @创建者 minifly.
 * @创建时间 2017/10/25 21:19
 * @描述 维护作业点第一个页面。     三顿饭胜多负少发
 */

public class ChooseJobUnitAddFirstpageFragment5 extends BaseFragment {

    private View mView;
    private ImageView mIvPlusStart;
    private RelativeLayout mIvPlusStartRel;
    private String jobPointUkid,warehouseUkid;
    private ElasticScrollView mEsvElastic;
    private boolean isCanfinish;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_protect_task_unit, container, false);
        initView(mView);
        return mView;
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ChooseJobUnitAddFirstpageFragment5) {
            mActivity.setTitle("" + getString(R.string.new_task_step5_judege_unit));
        }
    }

    @Override
    public void requestDatas() {

    }

    private void initView(View mView) {
        mEsvElastic = (ElasticScrollView) mView.findViewById(R.id.esv_elastic);

        jobPointUkid = getArguments().getString("jobPointUkid");
        warehouseUkid = getArguments().getString("warehouseUkid");

        mIvPlusStart = (ImageView) mView.findViewById(R.id.iv_plus_start);
        mIvPlusStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("jobPointUkid", jobPointUkid);
                bundle.putString("warehouseUkid", warehouseUkid);
                ChooseJobUnitAddItemBytwoWayFragment fragment = new ChooseJobUnitAddItemBytwoWayFragment();
                fragment.setArguments(bundle);
                pushFragment(fragment);
            }
        });
        mIvPlusStartRel = (RelativeLayout) mView.findViewById(R.id.iv_plus_start_rel);
        mIvPlusStartRel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("jobPointUkid", jobPointUkid);
                bundle.putString("warehouseUkid", warehouseUkid);
                ChooseJobUnitAddItemBytwoWayFragment fragment = new ChooseJobUnitAddItemBytwoWayFragment();
                fragment.setArguments(bundle);
                pushFragment(fragment);
            }
        });

        mEsvElastic.setOnPullListener(new ElasticScrollView.OnPullListener() {
            @Override
            public void onDownPull() {

                ((BaseCardDeskActivity)mActivity).setTitleBt1Text("完成此步");
                ((BaseCardDeskActivity)mActivity).showTitleBt1();
                ((BaseCardDeskActivity)mActivity).downPull_3();
                if(BaseApplication.sp.getBooleanValue("isCanfinish")){
                    ((BaseCardDeskActivity)mActivity).setTitleBt1Enable(true);
                }else{
                    ((BaseCardDeskActivity)mActivity).setTitleBt1Enable(false);
                }
            }
            @Override
            public void onUpPull() {
            }
        });
        EventBus.getDefault().register(this);

    }

    @Override
    public void titleBt1Click() {
        if(peekFragment() instanceof ChooseJobUnitAddFirstpageFragment5  && BaseApplication.sp.getBooleanValue("isCanfinish",false)){
            saveStep();
        }
    }

    /**
     * 保存步骤
     */

    public void saveStep(){
        Map<String,String> map  = new HashMap<>();
        map.put("jobPointUKid",jobPointUkid);
        map.put("step","5");
        /**
         * jobPointUKid	作业点ukid	number	@mock=100000110
         step	步骤，第二步，第四步，第五步需要调用	number	@mock=2
         */
        NoHttpUtils.httpPost(TaskCenterConstant.URL_JOBPOINT_SAVESTEP,map,myResponse,10001);
    }
    NoHttpUtils.OnResponseListener myResponse = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int i) {
            switch (i){
                case 10001:
                    if("0".equals(commonClass.getCode())){
                        BaseApplication.sp.putBooleanValue("isCanfinish",false);
                        TaskPointCardEvent event = new TaskPointCardEvent();
                        event.setFinish5(true);
                        EventBus.getDefault().post(event);
                        ((BaseCardDeskActivity)mActivity).cleanBackStackWithLeftNumber(1);
                    }else{
                        ToastUtils.showToast("" + commonClass.getMsg());
                    }
                    break;
            }

        }

        @Override
        public void onFailed(String s, int i) {
        }

        @Override
        public void onFinish(int i) {
            dismissProgressDialog();
        }
    };


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    public void onEventMainThread(BackListenerEvent event) {
        if (peekFragment() instanceof ChooseJobUnitAddFirstpageFragment5 && "ChooseJobUnitAddFirstpageFragment5".equals(event.getMsg())) {
            ((BaseCardDeskActivity) mActivity).cleanBackStackWithLeftNumber(1);
        }
    }
}
